package com.howie.parnote.validator.consumer;

import com.howie.parnote.dao.LocationModuleMapper;
import com.howie.parnote.dao.RoleModuleMapper;
import com.howie.parnote.module.LocationModule;
import com.howie.parnote.module.RoleModule;
import com.howie.parnote.util.StringUtil;
import com.howie.parnote.validator.bean.note.FormNoteBean;
import com.howie.parnote.validator.bean.note.ServiceNoteBean;
import com.howie.parnote.dao.IncidentModuleMapper;
import com.howie.parnote.util.security.DESCoder;
import com.howie.parnote.module.IncidentModule;
import com.howie.parnote.validator.annotation.CheckTag;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * <p>Title: parnote</p>
 * <p>
 * Description: 对note的标签进行验证
 * </p>
 *
 * @author huangyan
 *         Created by HY on 2015/2/11 0011.
 * @version v1.0.0
 */
public class CheckTagValidator implements ConstraintValidator<CheckTag, FormNoteBean> {

	@Override
	public void initialize(CheckTag constraintAnnotation) {

	}

	@Override
	public boolean isValid(FormNoteBean note, ConstraintValidatorContext context) {
		if (note == null) {
			return false;
		}

		String encodedRoleId = note.getRole();
		String encodedLocationId = note.getLocation();
		String encodedIncidentId = note.getIncident();
		String roleName = note.getRoleName();
		String locationName = note.getLocationName();
		String incidentName = note.getIncidentName();

		// service层应用bean
		ServiceNoteBean serviceBean = new ServiceNoteBean();
		note.setServiceNoteBean(serviceBean);

		try {
			//标签方式合法
			if (legalTag(encodedRoleId, encodedLocationId, encodedIncidentId, roleName, locationName, incidentName)) {
				//id解密
				Integer roleId = DESCoder.getHexDecodeId(encodedRoleId);
				Integer locationId = DESCoder.getHexDecodeId(encodedLocationId);
				Integer incidentId = DESCoder.getHexDecodeId(encodedIncidentId);
				// 外键标签不为空
				if (systemTag(encodedRoleId, encodedLocationId, encodedIncidentId)) {
					RoleModule roleModule = roleModuleMapper.selectByPrimaryKey(roleId);
					if (roleModule == null) {
						return false;
					}
					LocationModule locationModule = locationModuleMapper.selectByPrimaryKey(locationId);
					if (locationModule == null) {
						return false;
					}
					IncidentModule incidentModule = incidentModuleMapper.selectByPrimaryKey(incidentId);
					if (incidentModule == null) {
						return false;
					}
					serviceBean.setRoleModule(roleModule);
					serviceBean.setLocationModule(locationModule);
					serviceBean.setIncidentModule(incidentModule);
					return true;
				} else {
					/**
					 * 用户自定义标签,但有可能由于前端操作没有将ID关联
					 * 这里再做一次判断和处理
					 */

					if (StringUtil.isEmpty(encodedRoleId)) {
						RoleModule exist = roleModuleMapper.findByName(roleName);
						if (exist != null) {
							serviceBean.setRoleModule(exist);
						} else {
							serviceBean.setCustomRole(true);
						}
					} else {
						RoleModule roleModule = roleModuleMapper.selectByPrimaryKey(roleId);
						serviceBean.setRoleModule(roleModule);
					}
					if (StringUtil.isEmpty(encodedLocationId)) {
						LocationModule exist = locationModuleMapper.findByName(locationName);
						if (exist != null) {
							serviceBean.setLocationModule(exist);
						} else {
							serviceBean.setCustomLocation(true);
						}
					} else {
						LocationModule locationModule = locationModuleMapper.selectByPrimaryKey(locationId);
						serviceBean.setLocationModule(locationModule);
					}
					if (StringUtil.isEmpty(encodedIncidentId)) {
						IncidentModule exist = incidentModuleMapper.findByName(incidentName);
						if (exist != null) {
							serviceBean.setIncidentModule(exist);
						} else {
							serviceBean.setCustomIncident(true);
						}
					} else {
						IncidentModule incidentModule = incidentModuleMapper.selectByPrimaryKey(incidentId);
						serviceBean.setIncidentModule(incidentModule);
					}
					return true;
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return false;
	}

	/**
	 * bean填写的为系统tag code
	 *
	 * @param noteCode
	 * @param locationCode
	 * @param incidentCode
	 * @return
	 */
	private boolean systemTag(String noteCode, String locationCode, String incidentCode) {
		return StringUtil.notEmpty(noteCode) && StringUtil.notEmpty(locationCode) && StringUtil.notEmpty(incidentCode);
	}

	/**
	 * 每种标签只能存在异或关系
	 *
	 * @param encodedRoleId
	 * @param encodedLocationId
	 * @param encodedIncidentId
	 * @param roleName
	 * @param locationName
	 * @param incidentName      @return
	 */
	private boolean legalTag(String encodedRoleId, String encodedLocationId, String encodedIncidentId, String roleName, String locationName, String incidentName) {
		return (StringUtil.notEmpty(encodedRoleId) ^ StringUtil.notEmpty(roleName)) && (StringUtil.notEmpty(encodedLocationId) ^ StringUtil.notEmpty(locationName)) && (StringUtil.notEmpty(encodedIncidentId) ^ StringUtil.notEmpty(incidentName));
	}

	@Autowired
	private RoleModuleMapper roleModuleMapper;
	@Autowired
	private LocationModuleMapper locationModuleMapper;
	@Autowired
	private IncidentModuleMapper incidentModuleMapper;
}
